package org.dromara.web.controller.app;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.dao.business.domain.SmartAgent;
import org.dromara.dao.business.domain.SmartAgentCategory;
import org.dromara.web.controller.app.req.*;
import org.dromara.web.domain.app.SmartAgentDetailVO;
import org.dromara.web.service.business.SmartAgentCategoryService;
import org.dromara.web.service.business.SmartAgentService;
import org.dromara.web.service.business.SmartAgentSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *  智能体相关
 *
 */
@RequestMapping("/app/smartAgent")
@RestController
public class AppSmartAgentController {

    @Autowired
    SmartAgentCategoryService smartAgentCategoryService;
    @Autowired
    private SmartAgentService smartAgentService;
    @Autowired
    SmartAgentSettingService smartAgentSettingService;


    /**
     *  智能体分类列表
     *
     * @return
     */
    @Operation
    @GetMapping("/category")
    public R<List<SmartAgentCategory>> category(@RequestHeader("Content-Language")String lang) {

        return R.ok(smartAgentCategoryService.getValidCategory(lang));
    }


    /**
     * 智能体列表
     *
     * @param categoryId
     * @return
     */
    @Operation
    @Parameter(name = "categoryId", description = "分类ID， 不分类则传空")
    @Parameter(name = "search", description = "搜索字段")
    @GetMapping("/agent")
    public R<List<SmartAgent>> smartAgent(Long categoryId, String search, @RequestHeader("Content-Language")String lang) {
        return R.ok(smartAgentService.getSmartAgent(categoryId, search, lang, LoginHelper.getAppUserId()));
    }




    /**
     * 智能体详情
     *
     * @return
     */
    @Operation
    @Parameter(name = "agentId", description = "智能体ID")
    @GetMapping("/detail")
    public R<SmartAgentDetailVO> agentDetail(Long agentId,@RequestHeader("Content-Language")String lang) {

        return R.ok(smartAgentService.detail(agentId, lang));
    }


    /**
     *  设置全网搜索开关
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/setNetwork")
    public R<Void> updateAgentNetwork(@RequestBody AgentSwitchReq req) {

        smartAgentSettingService.updateNetworkSwitch(req.getAgentId(), LoginHelper.getAppUserId(), req.getStatus());
        return R.ok();
    }

    /**
     *  设置全网搜索开关
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/setImage")
    public R<Void> updateImageSwitch(@RequestBody AgentSwitchReq req) {

        smartAgentSettingService.updateImageSwitch(req.getAgentId(), LoginHelper.getAppUserId(), req.getStatus());
        return R.ok();
    }

    /**
     *  设置全网搜索开关
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/setData")
    public R<Void> updateAgentData(@RequestBody AgentSwitchReq req) {

        smartAgentSettingService.updateDataSwitch(req.getAgentId(), LoginHelper.getAppUserId(), req.getStatus());
        return R.ok();
    }

    @Operation
    @PostMapping("/setModel")
    public R<Void> updateAgentModel(@RequestBody AgentSetModelReq req) {

        smartAgentService.updateModel(req.getAgentId(), req.getModelId());
        return R.ok();
    }



    /**
     * 热门智能体
     *
     * @return
     */
    @Operation
    @GetMapping("/explore")
    public R<List<SmartAgent>> explore(@RequestHeader("Content-Language")String lang) {
        return R.ok(smartAgentService.queryHotAgent(LoginHelper.getAppUserId(), lang));
    }


    /**
     * 我的智能体列表
     *
     * @return
     */
    @Operation
    @GetMapping("/users")
    public R<List<SmartAgent>> users() {
        return R.ok(smartAgentService.queryUserAgentList(LoginHelper.getAppUserId()));
    }


    /**
     *  创建智能体
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/create")
    public R<Void> createAgent(@RequestBody SmartAgentCreateReq req) {
        smartAgentService.createAgent(req);
        return R.ok();
    }

    /**
     *  创建智能体
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/update")
    public R<Void> updateAgent(@RequestBody SmartAgentUpdateReq req) {
        smartAgentService.updateAgent(req);
        return R.ok();
    }


    /**
     *  设置智能体是否置顶
     *
     * @param req
     * @return
     */
    @Operation
    @PostMapping("/setTop")
    public R<Void> setAgentTop(@RequestBody AgentTopReq req) {
        smartAgentSettingService.updateAgentTop(req.getAgentId(), LoginHelper.getAppUserId(), req.getTop());
        return R.ok();
    }


    /**
     *  删除智能体
     *
     * @return
     */
    @Operation
    @PostMapping("/delete")
    public R<Void> deleteAgent(@RequestBody SmartAgentDeleteReq req) {
        smartAgentService.deleteAgent(req.getAgentId(), LoginHelper.getAppUserId());
        return R.ok();
    }
}
